#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIView-Geometry.h>
#import "MTTetrominosPreviewPane.h"
#import "MTTetrominosBoard.h"

@implementation MTTetrominosPreviewPane : UIView

-(id) initWithFrame: (CGRect)frame
{
	self = [super initWithFrame: frame];
	_currPiece = nil;
	
	CGRect previewPaneRect = frame;
	previewPaneRect.size.height = previewPaneRect.size.width;
	
	int i, j;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			CGRect cellRect;
			cellRect.size.width = previewPaneRect.size.width / 4.0f;
			cellRect.size.height = previewPaneRect.size.height / 4.0f;
			cellRect.origin.x = cellRect.size.width * i;
			cellRect.origin.y = cellRect.size.height * j;
			_previewCellImage[i][j] = [[UIImageView alloc] initWithFrame: cellRect];

			[self addSubview: _previewCellImage[i][j]];
		}
	}
	
	return self;
}

-(void) setPiece: (MTTetrominosPiece*)piece
{
	_currPiece = piece;
}

-(void) paint
{
	if (_currPiece != nil)
	{
		int i, j;
		for (i = 0; i < 4; i++)
		{
			for (j = 0; j < 4; j++)
			{
				switch([_currPiece getMask: i: j])
				{
					case NOPIECE:		[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"transparent.png"]]; break;
					case CYAN:			[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"CyanSquare.png"]]; break;
					case BLUE:			[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"BlueSquare.png"]]; break;
					case ORANGE:		[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"OrangeSquare.png"]]; break;
					case YELLOW:		[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"YellowSquare.png"]]; break;
					case GREEN:			[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"GreenSquare.png"]]; break;
					case PURPLE:		[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"PurpleSquare.png"]]; break;
					case RED:			[_previewCellImage[i][j] setImage: [UIImage applicationImageNamed: @"RedSquare.png"]]; break;
					default: break;
				}
			}
		}
	}
}



@end